﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace Saturn.App.Common
{
    public class CommonCfg
    {
        public static string ProductLine;

        public static string UserName;
        public static string UserId;
        public static string Userrealname;
        public static string orgId;
        public static string orgname ;

        public static string Location;
        public static string Locationid;

        public static string lastNumber;
        public static string DateTime;


        public static bool Update( string _param, string _value)
        {
            try
            {
                if (!CommonConfig.Instance().Update(_param, _value))
                {
                    return false;
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool GetSystemConfig()
        {
            try
            {
                foreach (var dataReader in CommonConfig.Instance().m_list)
                {
                    if (dataReader.Key == "productline")
                    {
                        ProductLine = dataReader.Value;
                    }
                    else if (dataReader.Key == "lastnumber")
                    {
                        if (!String.IsNullOrEmpty(dataReader.Value))
                        {
                            lastNumber = dataReader.Value;
                        }
                        else
                        {
                            lastNumber = "0";
                        }
                    }
                    else if (dataReader.Key == "location")
                    {
                        Location = dataReader.Value;
                    }
                    else if (dataReader.Key == "locationid")
                    {
                        Locationid = dataReader.Value;
                    }
                    else
                    {
                        //Log.Error(m_className, "参数错误：" + dataReader.Key + "," + dataReader.Value);
                    }

                }
                
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        public static string CreateBillCode(string endtime, string equipmentcode, EnumClass.EquipmentType equipmenttype)
        {
            return endtime + equipmentcode + CreateEquipmenttype(equipmenttype) + CreateGuid();
        }
        public static string CreateGuid()
        {
            int lastnumber = Int16.Parse(CommonCfg.lastNumber);
            if (lastnumber < 999)
            {
                lastnumber = lastnumber + 1;
                CommonCfg.lastNumber = lastnumber.ToString();
            }
            else
            {
                lastnumber = 1;
                CommonCfg.lastNumber = lastnumber.ToString();
            }
            Update("lastnumber", lastnumber.ToString());
            return lastnumber.ToString("d3");
        }

        public static string CreateEquipmenttype(EnumClass.EquipmentType equipmenttype)
        {
            string res = "";
            switch (equipmenttype)
            {
                case EnumClass.EquipmentType.Wx:
                    res= "1";
                    break;
                case EnumClass.EquipmentType.By:
                    res= "2";
                    break;
                case EnumClass.EquipmentType.Dj:
                    res= "3";
                    break;
                case EnumClass.EquipmentType.Rh:
                    res= "4";
                    break;
                default:
                    break;
            }
            return res;
        }
    }
}
